/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.pojo.sqlServer.PPInventory;
import me.zhengjie.ppService.PPInBoundService;
import me.zhengjie.pojo.NullPalletDto;
import me.zhengjie.pojo.Resp;
import me.zhengjie.pojo.sqlServer.PpPallet;
import me.zhengjie.repository.PPStorageRepository;
import me.zhengjie.service.PpPalletService;
import me.zhengjie.service.dto.PPOutBoundDto;
import me.zhengjie.service.dto.PpPalletDto;
import me.zhengjie.service.dto.PpPalletQueryCriteria;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;

/**
 * @author xinglin
 * @website https://eladmin.vip
 * @date 2024-08-16
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "pp载盘管理")
@RequestMapping("/api/ppPallet")
public class PpPalletController {

    private final PpPalletService ppPalletService;
    private final me.zhengjie.ppService.PpPalletService domainPalletService;
    private final PPInBoundService ppInBoundService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('ppPallet:list')")
    public void exportPpPallet(HttpServletResponse response, PpPalletQueryCriteria criteria) throws IOException {
        ppPalletService.download(ppPalletService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询pp载盘")
    @ApiOperation("查询pp载盘")
    @PreAuthorize("@el.check('ppPallet:list')")
    public ResponseEntity<PageResult<PpPalletDto>> queryPpPallet(PpPalletQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(ppPalletService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增pp载盘")
    @ApiOperation("新增pp载盘")
    @PreAuthorize("@el.check('ppPallet:add')")
    public ResponseEntity<Object> createPpPallet(@Validated @RequestBody PpPallet resources) {
        ppPalletService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改pp载盘")
    @ApiOperation("修改pp载盘")
    @PreAuthorize("@el.check('ppPallet:edit')")
    public ResponseEntity<Object> updatePpPallet(@Validated @RequestBody PpPallet resources) {
        ppPalletService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除pp载盘")
    @ApiOperation("删除pp载盘")
    @PreAuthorize("@el.check('ppPallet:del')")
    public ResponseEntity<Object> deletePpPallet(@RequestBody Integer[] ids) {
        ppPalletService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

//    @PostMapping("/OutNullPallet/{stationCode}")

    @PostMapping("/OutNullPallet")
    @Log("呼叫载盘出库")
    @ApiOperation("呼叫载盘出库")
    public Resp OutNullPallet(@RequestBody PPOutBoundDto ppOutBoundDto) {
        try {
            domainPalletService.outBoundPallet(ppOutBoundDto);
            return Resp.builder()
                    .status(200)
                    .msg("载盘出库成功")
                    .build();
        } catch (Exception e) {
            return Resp.builder()
                    .status(500)
                    .msg(e.getMessage())
                    .build();
        }

    }


    @PostMapping("/InNullPallet")
    @Log("空载盘回库")
    @ApiOperation("空载盘回库")
    public Resp InNullPallet(@RequestBody NullPalletDto nullPalletDto) {

        try {
            //判断空载盘编码是否为空
            if (StringUtils.isBlank(nullPalletDto.getPalletCode())) {
                return Resp.builder().status(501).msg("载盘编码不能为空").build();
            }

            if (StringUtils.isBlank(nullPalletDto.getStationCode())) {
                return Resp.builder().status(502).msg("工作站编码不能为空").build();
            }
            // 判断库存中是否已经存在这个载盘
            if (ppInBoundService.ifExistPallet(nullPalletDto.getPalletCode())){
                return Resp.builder().status(503).msg("已存在该载盘的入库任务").build();
            }
            domainPalletService.inBoundPallet(nullPalletDto.getPalletCode(), nullPalletDto.getStationCode());


            return Resp.builder()
                    .status(200)
                    .msg("空载盘回库")
                    .build();
        } catch (Exception e) {
            return Resp.builder()
                    .status(500)
                    .msg(e.getMessage())
                    .build();
        }

    }


}